package com.pr.entity;

import com.pr.service.LifeService;
import com.xw.framework.ddd.types.Entity;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description:
 * @projectName:SpringSkill
 * @see:org.pr.springskill.entity
 * @author:LXC
 * @createTime:2024/4/21 13:48
 * @version:1.0
 */
@Getter
@Setter
public class Person extends Entity<Person.Context> {
    private String id;
    private String name;
    private Date birthDay;
    private String address;

    public Person(Context context) {
        super(context);
    }


    public Person(String id, String name, Date birthDay, String address) {
        super(null);
        this.id = id;
        this.name = name;
        this.birthDay = birthDay;
        this.address = address;
    }

    /**
     * 保存信息
     */
    public Object write(String id) {
        return new Object();
    }

    /**
     * 获取信息，填充当前对象
     */
    public void read(String id) {
    }

    /**
     * 填充对西那个
     */
    public void fillValue(Person person) {
        BeanUtils.copyProperties(person, this);
    }

    /**
     * 其他一些方法
     */
    public void otherMethod() {
    }

    public void savePerson(){
        LifeService lifeService = ctx().lifeService;
    }


    /*public static Factory getFactory() {
        return SpringContextUtil.getBean(Factory.NAME);
    }*/


    /**
     * 借助工厂实现依赖注入
     */
    @Component(Factory.NAME)
    public static class Factory implements Entity.Factory {

        private static Context context;
        public static final String NAME = "personFactory";

        public static Person make() {
            return new Person(context);
        }

        public Factory(
                Context context,
                LifeService lifeService
        ) {
            context.lifeService = lifeService;
            Factory.context=context;
        }

        public static Context getContext() {
            return context;
        }
    }

    /**
     * 使用Context来分离数据和服务
     */
    @Component
    public static class Context implements Entity.Context {
        private LifeService lifeService;
    }

}
